package com.explorati.LeetCode104.maxDepth;

/**
 * @ Author : Weijian_Wang
 * @ Date : Created in 23:01 2020/9/26 0026
 * @ Description ：
 */
public class Solution1 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    /**
     * 给定一个二叉树，找出其最大深度。
     * @param root
     * @return
     */
    public int maxDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }else {
            /**
             * 遍历每个节点的左右两侧，返回每个节点左右节点最大高度
             */
            int leftHeight = maxDepth(root.left);
            int rightHeight = maxDepth(root.right);
            return Math.max(leftHeight, rightHeight) + 1;
        }
    }
}
